<?php

class productgallery {
/**
*   @desc Variable Declaration with default value
*/
	private $_id;   // KEY ATTR. WITH AUTOINCREMENT
     private $_pid;  
     private $_image;  
     private $_createddate;  
     
/**
*   @desc   CONSTRUCTOR METHOD
*/
	function __construct() {
		global $DBOBJ;
		$this->_obj = $DBOBJ;
		$this->_id = null; 
		$this->_pid = null; 
		$this->_image = null; 
		$this->_createddate = null; 
	}

/**
*   @desc   DECONSTRUCTOR METHOD
*/
	function __destruct() {
		unset($this->_obj);
	}

/**
*   @desc   GETTER METHODS
*/

     public function getid() {
     	return $this->_id;
     }
     
     public function getpid() {
     	return $this->_pid;
     }
     
     public function getimage() {
     	return $this->_image;
     }
     
     public function getcreateddate() {
     	return $this->_createddate;
     }
     
/**
*   @desc   SETTER METHODS
*/

     public function setid($id) {
     	 $this->_id =  $id;
     }
     
     public function setpid($pid) {
     	 $this->_pid =  $pid;
     }
     
     public function setimage($image) {
     	 $this->_image =  $image;
     }
     
     public function setcreateddate($createddate) {
     	 $this->_createddate =  $createddate;
     }
     
/**
*   @desc   SELECT METHOD / LOAD
*/

	public function select($id) {
		 $sql =  "SELECT * FROM `productgallery` WHERE `id` = '". $id ."' ";
		 $row =  $this->_obj->select($sql); 

		 if (count($row) > 0) {
			 $this->_id = $row[0]['id'];
     		 $this->_pid = $row[0]['pid'];
     		 $this->_image = $row[0]['image'];
     		 $this->_createddate = $row[0]['createddate'];
     			 return $row[0];
		}else {
			 return;
		}
	}
/**
*   @desc   set All
*/
	 public function setAll($dataArray = array()) {
		 if (count($dataArray) <= 0) {
			return;
		}

		foreach ($dataArray as $key => $value) {
			$classKey = '_' . $key;
			$this->$classKey = $value;
		}
	}

/**
*   @desc   get All
*/

	 public function getAll($FORMAT = 'ARRAY') {
		$dataArray = new stdClass();
		$dataArray->id = $this->getid();
     	$dataArray->pid = $this->getpid();
     	$dataArray->image = $this->getimage();
     	$dataArray->createddate = $this->getcreateddate();
     	if ($FORMAT == 'ARRAY') {
			return (array) $dataArray;
		}
		return $dataArray;
	}
/**
*   @desc   COUNT
*/

	public function getCountproductgallery() {
		 $sql = "SELECT count(*) as cnt  FROM `productgallery`";
		 $result = $this->_obj->select($sql);
		 return $result['0']['cnt'];
	}

/**
*   @desc   DELETE
*/
	public function delete($id) {
		 $sql = "DELETE FROM `productgallery` WHERE `id` = '". $id ."'";
		 $result = $this->_obj->sql_query($sql);
	}

/**
*   @desc   INSERT
*/
	public function insert() {
		 $this->id = ""; // clear key for autoincrement
		 $sql = "INSERT INTO `productgallery` ( `id`,`pid`,`image`,`createddate` ) VALUES ( NULL,'".$this->_pid."','".$this->_image."','".$this->_createddate."' )";
		 $result = $this->_obj->insert($sql);
		 return $result;
	}

/**
*   @desc   UPDATE
*/
	public function update($id) {
		 $sql = " UPDATE `productgallery` SET  `id` = '".$this->_id."' , `pid` = '".$this->_pid."' , `image` = '".$this->_image."' , `createddate` = '".$this->_createddate."'  WHERE `id` =   '".$id."' ";
		 $result = $this->_obj->sql_query($sql);
	}
	public function getAllProductgalleryList($params = "*", $where = "", $orderby = "id", $limit = "") {
		$orderby = "ORDER BY " . $orderby;
		$sql = "SELECT " . $params . " FROM `productgallery` a  " . $where . " " . $orderby . " " . $limit;
		$row = $this->_obj->select($sql);
		if ($row) {
			return $row;
		}
		return array();
	}
	
	public function performListAction($action = "", $ids = "''") {
		switch ($action) {
			case "delete":
				$sql = "DELETE FROM `productgallery` WHERE `id` IN (" . $ids . ")";
			break;
			default:
				$sql = "";
		}
		if ($sql == "") {
			return false;
		}
		$result = $this->_obj->sql_query($sql);
		return true;
    }
	

	public function updateImage($id = "", $filename = "") {
		if ($id == "") {
			return false;
		}
		$updateImageQuery = "UPDATE productgallery SET  image = '" . $filename . "' WHERE `id` = '" . $id . "'";
		$this->_obj->sql_query($updateImageQuery);
    }


	#################
	public function deleteImage($name) {
		$sourceFolder = "../upload/images/product/";
		$imagepath = $sourceFolder . trim($name);
		$thumb_imagepath = $sourceFolder . trim("thumb_".$name);
		$medium_imagepath = $sourceFolder . trim("medium_".$name);
		
		if (file_exists($imagepath)) {
			@unlink($imagepath);
		}
		
		if (file_exists($thumb_imagepath)) {
			@unlink($thumb_imagepath);
		}
		
		if (file_exists($medium_imagepath)) {
			@unlink($medium_imagepath);
		}
		
		

	}


	
	

} 
?>